<?php
namespace Admin\Controller;

use Think\Controller;

class bannerController extends CommonController
{
    public function index()
    {  
        $this->assign('url',U('banner/add'));
        $this->display();
    }
    
    public function view(){
        $banner = D('banner');
        $b = $banner->select();
        $this->assign('list', $b);      
        $this->display();
    }
    
  public function edit(){
      $id=I('get.id');
      $where['id']=$id;
      $b=D('banner')->where($where)->find();
      $this->assign('url',U('banner/doedit'));
      $this->assign('b',$b);
      $this->display('index');
  }
    public function add(){
        $bannerr = D('banner');
        $data = $bannerr->create();
        if ($_FILES['apic']['tmp_name'] != '') {
            $upload = new \Think\Upload();
            $upload->maxSize = 3145728;
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
            $upload->rootPath = './Uploads/';
            $upload->savePath = '/';
            $info = $upload->uploadOne($_FILES['apic']);
            if (!$info) {
                $this->error($upload->getError());
            } else {
                $data['apic'] = $info['savepath'] . $info['savename'];
            }
        }
        $result = $bannerr->add($data);
        if ($result>0 ) {
            $this->success('添加成功！', U('view'));
        } else {
            $this->error('添加失败！');
        }       
    }
    public function doedit()
    {
        $banner = D('banner');
        $data = $banner->create();
        if ($_FILES['apic']['tmp_name'] != '') {
            $upload = new \Think\Upload();
            $upload->maxSize = 3145728;
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
            $upload->rootPath = './Uploads/';
            $upload->savePath = '/';
            $info = $upload->uploadOne($_FILES['apic']);
            if (!$info) {
                $this->error($upload->getError());
            } else {
                $data['apic'] = $info['savepath'] . $info['savename'];
            }
        }       
        $result = $banner->save($data);
        if (false !== $result || 0 !== $result) {
            $this->success('修改成功！', U('view'));
        } else {
            $this->error('修改失败！');
        }
    }
    public function delete(){
        $id=I('get.id');
        $where['id']=$id;
        $b=D('banner')->where($where)->delete();
        if ($b){
            $this->success('删除成功！');
        }else {
            $this->error('删除失败！');
        }
    }
    public function deletes(){
        $id=I('post.deletes');
       
        $map['id']=array('in',$id);
        $m=D('banner')->where($map)->delete();
        if(empty($id)){
            $this->error('没有选择id！');
        }else {
            if ($m){
                $this->success('批量删除成功！');
            }else {
                $this->error('批量删除失败！');
            }
        }
       
    }
    
}